const fs = require('fs');

function base64ToFile(base64, name) {
    return new Promise((res, rej) => {
        try {
            const base64Data = base64.base64.replace(/^data:image\/\w+;base64,/, "");
            // 生成路由
            const url = __dirname.split('\\utils')[0] + "\\public\\images\\"
            const type = "." + base64.type.split('/')[1]
            // 将 base64 字符串解码为二进制数据
            const buffer = Buffer.from(base64Data, 'base64');
            // // 保存为图像文件
            fs.writeFile(url + name + type, buffer, (err) => {
                if (err) return rej(err)
                res("/images/" + name + type)
            });
        } catch (e) {
            rej(e)
        }

    })
}


module.exports = base64ToFile
